//
//  PxBaseOilNetCmd.m
//  OilCard
//
//  Created by xuyf on 14-5-13.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxBaseOilNetCmd.h"

#import "NSString+Additions.h"
#import "LLSmallUtil.h"
#import "PxNetSession.h"

@implementation PxBaseOilNetCmd

- (NSDictionary*)functionInfoDic
{
    return @{
             // functionName:[transcode, url, retIsList, needLogin]
             OAUTH_CODE_CALLBACK:@[@"method", @"/authorization_code_callback"],
             
             TRANS_DIC_INFO:@[@"method", @"/dictionary/query"],
             TRANS_STORE_INFO:@[@"method", @"/store/expressCompany"],
             TRANS_SMSMODEL_INFO:@[@"method", @"/sms/queryModel"],
             
             TRANS_SMS_DEL:@[@"method", @"/sms/deleteFailSmsRecords"],
             TRANS_SMS_SEND:@[@"method", @"/sms/scanAndSendSms"],
             TRANS_SMS_RESEND:@[@"method", @"/sms/sendSmsAgain"],
             TRANS_SMS_FAILD:@[@"method", @"/sms/queryFailSmsRecords"],
             
             TRANS_WAYBILL_INFO_UPLOAD:@[@"method", @"/wayBill/infoUpload"],
             TRANS_WAYBILL_INFO_QUERY:@[@"method", @"/wayBill/queryInfo"],
             
             TRANS_USER_LOGIN:@[@"method", @"/employee/login"],
             TRANS_USER_REGISTER:@[@"method", @"/employee/register"],
             TRANS_USER_INFO:@[@"method", @"/user/info"],
             TRANS_USER_PWD_FIND:@[@"method", @"/employee/findPassword"],
             
             TRANS_USER_SMS_SEND:@[@"method", @"/validateCode/send"],
             TRANS_USER_PWD_MODIFY:@[@"method", @"changePassword"],
             TRANS_USER_INFO_MODIFY:@[@"method", @"modifyUserInfo"],
             
             TRANS_VERSION_CHECK:@[@"method", @"/dictionary/query"],
             TRANS_RECIPIENT_STATUS:@[@"method", @"/recipientstatus/notice"],
             
             TRANS_WAYBILL_RECIPIENT:@[@"method", @"/waybill/recipient"],
             TRANS_WAYBILL_LIST:@[@"method", @"/waybill/list"],
             TRANS_WAYBILL_SIGN:@[@"method", @"/waybill/sign"],
             TRANS_WAYBILL_INFO:@[@"method", @"/waybill/info"],
             TRANS_WAYBILL_NOTIFYSTATUS:@[@"method", @"/waybill/notice/status"],
            };
}

- (NSString*)signature
{
//    NSMutableArray *keyArray = [NSMutableArray arrayWithArray:[self.paramDic allKeys]];
//    [keyArray sortUsingSelector:@selector(localizedCompare:)];
    
    // 按照算法sign_key 不参与排序
//    [keyArray addObject:[self signKeyTitle]];
    
    NSArray *keyArray = @[@"loginContent",@"compNo",@"certificateNo",@"userId",@"startDate"];
    
    NSString *sortedParamString = [self paramStringWithKeyArray:keyArray];
    
    NSString *signature = [sortedParamString MD5String];
    
    return signature;
}

- (NSString*)paramStringWithKeyArray:(NSArray*)keyArray
{
    NSMutableString *paramString = [NSMutableString stringWithString:@""];
    
    for (NSString *key in keyArray)
    {
        if ([[self valueWithKey:key] length]!=0){
            [paramString appendFormat:@"%@%@", key, [self valueWithKey:key]];
        }
    }
    
    return paramString;
    
}

- (void)prepareForSend
{
    NSString *functionName = self.functionName;
    
    NSArray *transInfoDic = [self functionInfoDic][functionName];

    self.pathUrl = [NSString stringWithFormat:@"%@%@", [PxGlobal getConfig:@"RootPath"], transInfoDic[1]];
    if ([PxNetSession isUserLogin]) {
        PxAccessToken *token = [PxNetSession sharedSession].token;
        [self.paramDic setValue:token.access_token forKey:ACCESS_TOKEN];
    }
    
    [self.paramDic setValue:[PxNetSession sharedSession].currentUser.tokenId forKey:@"tokenId"];
    
//    [self prepareParamDic];
    
//    [self urlEncodeParamDic];
    
//    [self.paramDic setValue:[self signature] forKey:[self signatureTitle]];
}

- (NSString*)signKeyTitle
{
    return @"sign_key";
}

- (NSString*)signatureTitle
{
    return @"signValue";
}

- (BOOL)isUseCmdResultParser
{
    return YES;
}

- (void)handleResultObj:(id)jsonObj
{
    NSString *jsonString = [[NSString alloc] initWithData:jsonObj
                                                 encoding:NSUTF8StringEncoding];;
    
    NSString *jsonNoEscString = [jsonString stringByRemovingPercentEncoding];
    
    NSDictionary *jsonNoEscObj = [jsonNoEscString JSONObject];
    
    LLDDLog(@"%@", jsonNoEscObj);
    
    if ([jsonNoEscObj[RESULT_CODE] isEqualToString:RESULT_SUCCESS])
    {
        if (self.businessSuccessHandler) {
            self.businessSuccessHandler(jsonNoEscObj);
        }
    }
    else
    {
        if ([jsonNoEscObj[RESULT_CODE] isEqualToString:RESULT_NOT_LOGIN]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login"                                                                object:nil];
            return;
        }
        if (self.businessErrorHandler){
            self.businessErrorHandler(jsonNoEscObj);
        }
    }
}
@end